package frame;

import clsaa.Biglotto;
import data.ConfigBigLotto;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static frame.MainFrame.initpanelTable;

public class findlottoFrame extends JFrame {

    static JPanel  dataShow;
    private JTextField frontArea1Field;
    private JTextField frontArea2Field;
    private JTextField frontArea3Field;
    private JTextField frontArea4Field;
    private JTextField frontArea5Field;
    private JTextField backArea1Field;
    private JTextField backArea2Field;

    public void initdataShow(int n){
        if(dataShow != null)dataShow.removeAll();
        dataShow.setBounds(0,400,400,400);
        // 创建一个滚动面板
        JScrollPane scrollPane = new JScrollPane();

        // 创建一个JTextArea用于显示文本
        JTextArea textArea = new JTextArea();
        textArea.setEditable(false); // 设置文本区域为不可编辑
        textArea.setLineWrap(true); // 设置自动换行
        textArea.setWrapStyleWord(true); // 设置在单词边界处换行
        // 将JTextArea添加到滚动面板中
        scrollPane.setViewportView(textArea);
        if(n==1){
            int[] dataA=new int[6];
            int[] dataB=new int[2];
            if(frontArea1Field.getText()==null || frontArea2Field.getText()==null || frontArea3Field.getText()==null || frontArea4Field.getText()==null || frontArea5Field.getText()==null || backArea1Field.getText()==null || backArea2Field.getText()==null){
                JOptionPane.showMessageDialog(null, "请输入");
                return;
            }
            if(frontArea1Field.getText().equals("") || frontArea2Field.getText().equals("") || frontArea3Field.getText().equals("") || frontArea4Field.getText().equals("") || frontArea5Field.getText().equals("") || backArea1Field.getText().equals("") || backArea2Field.getText().equals("")){
                JOptionPane.showMessageDialog(null, "请输入");
                return;
            }
            dataA[0] = Integer.parseInt(frontArea1Field.getText());
            dataA[1] = Integer.parseInt(frontArea2Field.getText());
            dataA[2] = Integer.parseInt(frontArea3Field.getText());
            dataA[3] = Integer.parseInt(frontArea4Field.getText());
            dataA[4] = Integer.parseInt(frontArea5Field.getText());
            for(int i=0;i<4;++i){
                for(int j=i+1;j<5;++j){
                    if(dataA[i]>dataA[j]){
                        int v=dataA[i];
                        dataA[i]=dataA[j];
                        dataA[j]=v;
                    }
                }
            }
            for(int i=0;i<5;++i){
                if(dataA[i]>35 || dataA[i]<=0){
                    JOptionPane.showMessageDialog(null, "前区号码范围有错误");
                    return;
                }
                if(dataA[i]==dataA[i+1]){
                    JOptionPane.showMessageDialog(null, "前区号码存在相同有错误");
                    return;
                }
            }

            if(Integer.parseInt(backArea1Field.getText())<=0 || Integer.parseInt(backArea2Field.getText())<=0 || Integer.parseInt(backArea1Field.getText())>12 || Integer.parseInt(backArea2Field.getText())>12){
                JOptionPane.showMessageDialog(null, "后区号码存在范围错误");
                return;
            }
            if(Integer.parseInt(backArea1Field.getText()) == Integer.parseInt(backArea2Field.getText())){
                JOptionPane.showMessageDialog(null, "后区号码存在相同有错误");
                return;
            }
            if(Integer.parseInt(backArea1Field.getText()) < Integer.parseInt(backArea2Field.getText())){
                dataB[0]=Integer.parseInt(backArea1Field.getText());
                dataB[1]=Integer.parseInt(backArea2Field.getText());
            }else{
                dataB[0]=Integer.parseInt(backArea2Field.getText());
                dataB[1]=Integer.parseInt(backArea1Field.getText());
            }
            // 示例：向文本区域添加一些文本

            textArea.removeAll();
            textArea.append("查找数据为："+dataA[0]+"  "+dataA[1]+"  "+dataA[2]+"  "+dataA[3]+"  "+dataA[4]+" + "+dataB[0]+"  "+dataB[1]+" ...... \n");
            ConfigBigLotto configBigLotto=new ConfigBigLotto();
            configBigLotto.getAllbiglotto().reorderAscending();
            int[] zhongjiang=new int[]{0,0,0,0,0,0,0,0,0};
            for(Biglotto biglotto:configBigLotto.getAllbiglotto().getAllData()){
                int what=biglotto.duibi(dataA[0],dataA[1],dataA[2],dataA[3],dataA[4],dataB[0],dataB[1]);
                if(what!=0){
                    textArea.append("找到"+what+"等奖:"+biglotto.ThistoString());
                    zhongjiang[what-1]++;
                }

            }
            textArea.append("在"+configBigLotto.getAllbiglotto().getAllData().size()+"条历史数据中寻找到：\n");
            textArea.append("一等奖："+zhongjiang[0]+"注  二等奖："+zhongjiang[1]+"注  三等奖："+zhongjiang[2]+"注  \n四等奖："+zhongjiang[3]+"注  五等奖："+zhongjiang[4]+"注  六等奖："+zhongjiang[5]+"注  \n七等奖："+zhongjiang[6]+"注  八等奖："+zhongjiang[7]+"注  九等奖："+zhongjiang[8]+"注\n");
        }


        // 创建一个主面板并使用BorderLayout
        dataShow.add(scrollPane, BorderLayout.CENTER);
        add(dataShow);

        dataShow.updateUI();
    }

    public findlottoFrame() {
        super("查找");

        // 设置窗口关闭操作
        setDefaultCloseOperation(2);
        // 设置窗口大小
        setSize(400, 850);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        // 计算JFrame的左上角应该放在屏幕上的位置
        int x = (screenSize.width - this.getWidth()) / 2;
        int y = (screenSize.height - this.getHeight()) / 2;

        // 设置JFrame的位置
        this.setLocation(x, y);
        setLayout(null);
        JPanel dataINJP=new JPanel();
        dataINJP.setLayout(null);
        dataINJP.setBounds(0,0,300,400);

        JLabel data1JL=new JLabel("前区1:");
        data1JL.setBounds(0,0,100,50);
        dataINJP.add(data1JL);

        frontArea1Field= new JTextField();
        frontArea1Field.setBounds(150,0,100,50);
        dataINJP.add(frontArea1Field);

        JLabel data2JL=new JLabel("前区2:");
        data2JL.setBounds(0,50,100,50);
        dataINJP.add(data2JL);

        frontArea2Field= new JTextField();
        frontArea2Field.setBounds(150,50,100,50);
        dataINJP.add(frontArea2Field);

        JLabel data3L=new JLabel("前区3:");
        data3L.setBounds(0,100,100,50);
        dataINJP.add(data3L);

        frontArea3Field= new JTextField();
        frontArea3Field.setBounds(150,100,100,50);
        dataINJP.add(frontArea3Field);

        JLabel data4JL=new JLabel("前区4:");
        data4JL.setBounds(0,150,100,50);
        dataINJP.add(data4JL);

        frontArea4Field= new JTextField();
        frontArea4Field.setBounds(150,150,100,50);
        dataINJP.add(frontArea4Field);

        JLabel data5JL=new JLabel("前区5:");
        data5JL.setBounds(0,200,100,50);
        dataINJP.add(data5JL);

        frontArea5Field= new JTextField();
        frontArea5Field.setBounds(150,200,100,50);
        dataINJP.add(frontArea5Field);

        JLabel data6JL=new JLabel("后区1:");
        data6JL.setBounds(0,250,100,50);
        data6JL.setBackground(new Color(0x31B1FF));
        data6JL.setForeground(new Color(0xFF2859));
        dataINJP.add(data6JL);

        backArea1Field= new JTextField();
        backArea1Field.setBounds(150,250,100,50);
        backArea1Field.setBackground(new Color(0xFF0A19));
        dataINJP.add(backArea1Field);

        JLabel data7JL=new JLabel("后区2:");
        data7JL.setBackground(new Color(0x31B1FF));
        data7JL.setForeground(new Color(0xFF2859));
        data7JL.setBounds(0,300,100,50);
        dataINJP.add(data7JL);

        backArea2Field= new JTextField();
        backArea2Field.setBounds(150,300,100,50);
        backArea2Field.setBackground(new Color(0xFF0A19));
        dataINJP.add(backArea2Field);

        JButton JBfin=new JButton("查找");
        JBfin.setBounds(100,350,100,50);
        JBfin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 取消按钮的逻辑（例如关闭窗口）
                initdataShow(1);
            }
        });
        dataINJP.add(JBfin);

        dataShow=new JPanel(new BorderLayout());
        initdataShow(0);
        add(dataShow);
        add(dataINJP);
        setVisible(true);
        new Thread(new findlottoFrame.NoWeightedGoods()).start();
    }

    public static void main(String[] args) {
        // 创建并显示窗口
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new findlottoFrame();
            }
        });
    }
    public class NoWeightedGoods implements Runnable{
        @Override
        public void run() {
            while(true){
                repaint();
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
